#!/usr/bin/perl
use warnings;
use strict;
my %chrs;
my %repeats;
my %repeatHash;
my @A;
my $minChrCount=$ARGV[0];
my $prefix=$ARGV[1];
my $nm;
print "using minChrCount\t$minChrCount\n";
while(<STDIN>) { 
chomp; 
@A=split(/\s+/,$_); 
$nm="$A[2]"."_"."$A[1]";
$chrs{$nm}=1;
$repeats{$A[4]}+=1;
$repeatHash{$nm}{$A[4]}=$A[10];
} 
open CHRS,"> $prefix.chrRowNames.txt" or die "bad open 1";
open REPS,"> $prefix.repColNames.txt" or die "bad open 2";
open MATRIX,"> $prefix.chrRepMatrix.txt" or die "bad open 3";
my $rep;
my $chr;
map {
  $chr=$_; 
  print CHRS $chr."\n";
  map { 
  $rep=$_;
  if ((exists($repeatHash{$chr}{$rep})) && ($repeats{$rep} >= $minChrCount)) {
     printf MATRIX ("%5.3f\t",$repeatHash{$chr}{$rep});
    printf("rep %s\n",$rep);
   } else {
     print MATRIX "nan\t"
   };
  } sort {$a cmp $b} keys %repeats; 
  print MATRIX "\n";
} sort { $a cmp $b } keys %chrs;

map {
  $rep=$_;
  print REPS "$rep\t$repeats{$rep}\n";
} sort {$a cmp $b} keys %repeats
